#!/bin/bash

# 传入本地cloudera父路径

while getopts ":d:t:u:v:h" arg
do
	case $arg in
		d)
			cdh_dir="$OPTARG"
			;;
		t)
			install_type="$OPTARG"
			;;
		u)
			update_ext="$OPTARG"
			;;
		v)	
			version="$OPTARG"
			;;
		h)
			echo -e  "Option:\n  -h show this help message and exit\n  -d cdh dir\n  /bin/bash install -d /opt/cloudera -t [all/csd/parcels] -u [true/false] -v [el7/el6]"
			exit 1
			;;
		?)
			echo "Contain unknown argument! Make sure transport argument is in list [-d -t -u -v -h]"
			exit 1
			;;
	esac
done

shift $(($OPTIND - 1))


current_path=$(cd $(dirname "${BASH_SOURCE[0]}") && pwd)

CLOUDERA_DIR=$cdh_dir

gen_manifest(){
	result=`grep "kibana-7.16.3"  $CLOUDERA_DIR/parcel-repo/manifest.json`
	if [[ ! $result =~ "kibana-7.16.3" || $update_ext == "true" ]];then
		/usr/bin/python $current_path/lib/make_manifest.py $CLOUDERA_DIR/parcel-repo/
	fi
}



gen_sha(){

        file_hash=`grep -2 "kibana-7.16.3"  $CLOUDERA_DIR/parcel-repo/manifest.json | awk '/hash/{print $2}' | sed 's/"//' | sed 's/",//'`
	echo $file_hash >  $CLOUDERA_DIR/parcel-repo/kibana-7.16.3-$version.parcel.sha

}


gen_parcels(){

        if [ ! -d $current_path/parcels/kibana-7.16.3/target ];then
		if [[ $version == "el6" ]];then
                	/bin/bash $current_path/parcels/kibana-7.16.3/build package6
		elif [[ $version == "el7" ]];then
			/bin/bash $current_path/parcels/kibana-7.16.3/build package7
		fi
        elif [ ! -f $current_path/parcels/kibana-7.16.3/target/kibana-7.16.3-$version.parcel ];then
                if [[ $version == "el6" ]];then
                        /bin/bash $current_path/parcels/kibana-7.16.3/build package6
                elif [[ $version == "el7" ]];then
                        /bin/bash $current_path/parcels/kibana-7.16.3/build package7
                fi        
	fi
}

gen_csd(){
	if [ ! -d $current_path/csd/kibana-7.16.3/target ];then
        	/bin/bash $current_path/csd/kibana-7.16.3/build package
	elif [ ! -f $current_path/csd/kibana-7.16.3/target/kibana-7.16.3.jar ];then
		/bin/bash $current_path/csd/kibana-7.16.3/build package
	fi
}



install_csd(){
	echo "gen_csd."
        gen_csd
	csd_dir=$CLOUDERA_DIR/csd/
	echo "install csd."
	cp -f $current_path/csd/kibana-7.16.3/target/kibana-7.16.3.jar $csd_dir
	chown cloudera-scm. -R $csd_dir/kibana-7.16.3.jar
	echo "done!"
}


install_parcels(){
	echo "gen_parcels."
	gen_parcels
	parcels_dir=$CLOUDERA_DIR/parcel-repo/
	echo "install parcels"
	
	cp -f $current_path/parcels/kibana-7.16.3/target/kibana-7.16.3-$version.parcel $parcels_dir
	echo "gen_manifest"
	gen_manifest
	echo "gen_sha"
	gen_sha
	chown cloudera-scm. -R $parcels_dir/kibana-7.16.3-$version.parcel
	chown cloudera-scm. -R $parcels_dir/kibana-7.16.3-$version.parcel.sha
	chown cloudera-scm. -R $parcels_dir/manifest.json
	echo "done!"
}

if [[ $install_type == "parcels" ]];then
        if [[ $update_ext == "true" ]];then
                /bin/bash $current_path/parcels/kibana-7.16.3/build clean
        fi
        install_parcels
elif [[ $install_type == "csd" ]];then
        if [[ $update_ext == "true" ]];then
                /bin/bash $current_path/csd/kibana-7.16.3/build clean
        fi
        install_csd
elif [[ $install_type == "all" ]];then
        if [[ $update_ext == "true" ]];then
                /bin/bash $current_path/parcels/kibana-7.16.3/build clean
                /bin/bash $current_path/csd/kibana-7.16.3/build clean
        fi
        install_parcels
        install_csd
else
        echo "please -t input install_type [parcels,csd,all] and -d input cdh dir"
fi
